<?php
namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\model\Order as OrderModel;
use hema\delivery\Driver as Delivery;

/**
 * 用户订单管理
 */
class Order extends Controller
{
    /**
     * 我的所有类型订单列表
     */
    public function all()
    {
        $model = new OrderModel;
        $list = [
            0 => $model->getList('afoot','',$this->user_id),//进行中
            1 => $model->getList('complete','',$this->user_id),//已完成
            2 => $model->getList('cancel','',$this->user_id),//已取消
        ];
        return $this->renderSuccess($list);
    }

    /**
     * 我的订单列表
     */
    public function lists(string $dataType)
    {
        $model = new OrderModel;
        $list = $model->getList($dataType,'',$this->user_id);
        return $this->renderSuccess($list);
    }
    
    /**
     * 订单详情信息
     */
    public function detail($order_id)
    {
        $order = OrderModel::detail($order_id);
        if($order['delivery_status']['value']==20){
            //获取骑手坐标
            $dv = new Delivery('hmpt');
            $post_data = [
                'order_no' => $order['address']['order_no'],//平台订单号
                //'shop_order_no' => $order['address']['order_no'],//第三方订单号
            ];
            if($result = $dv->riderPosition($post_data)){
    		    $order['rider'] = $result;
            }
        }
        return $this->renderSuccess($order);
    }
    
    /**
     * 取消订单
     */
    public function cancel($order_id)
    {
        $model = OrderModel::get($order_id);
        if ($model->cancel()) {
            return $this->renderMsg('操作成功');
        }
        $error = $model->getError() ?: '操作失败';
        return $this->renderError($error);
    }
    
    /**
     * 确认收货
     */
    public function receipt($order_id)
    {
        $model = OrderModel::get($order_id);
        if ($model->receipt()) {
            return $this->renderMsg('操作成功');
        }
		$error = $model->getError() ?: '操作失败';
        return $this->renderError($error);
    }

    /**
     * 立即支付
	 * $pay_mode 支付模式 1微信，2余额,3面对面，4支付宝
	 * $browser 浏览器类型，只有H5才会传递
     */
    public function payment($order_id, $pay_mode,$browser='')
    {
        // 订单详情
        $model = OrderModel::detail($order_id);
        if($data = $model->userPay($pay_mode,$this->mp,$browser)){
            return $this->renderSuccess($data);
        }
        $error = $model->getError() ?: '支付失败';
        return $this->renderError($error);
    }

     
}
